package tt.dz.egobus.servicemanager.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import tt.dz.egobus.api.common.AbstractFacadeFactoryService;
import tt.dz.egobus.servicemanager.core.config.ConfigAccess;
import tt.dz.egobus.servicemanager.core.config.DubboFacadeConvertConfigAccess;
import tt.dz.egobus.servicemanager.facade.common.CommonFacade;
import tt.dz.egobus.servicemanager.facade.interator.FacadeFactory;
import tt.dz.egobus.servicemanager.facade.interator.FacadeFactoryLoggerInterceptorHolder;

import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;

import static tt.dz.egobus.servicemanager.core.config.RuntimeConfigKeyEnum.*;

/**
 * @author Anson
 * @date 17-3-30
 * @name 当前配置了spring的配置信息，需要通过get方法获取
 * 主要获取的是properties/context.properties的配置信息
 * @since 1.0.3
 */
@Component("springConfigConstant")
public class SpringConfigConstant extends AbstractFacadeFactoryService {

    //appkey的前缀
    public static final String APP_COMMON_PRE = "EGOBUS";
    //redis过期管道key
    public static final String EXPIRE_REDIS_KEY = "__keyevent@0__:expired";
    //短信通知
    public static final String BUSINESS_MSG_NOTIFY_KEY = "BusinessMegNotify";


    @Override
    protected FacadeFactory getFacadeFactory() {
        return FacadeFactoryLoggerInterceptorHolder.LOGGER_FACADE;
    }

    //单例对象下配置引用
    private AtomicReference<ConfigAccess> configAccessAtomicReference = new AtomicReference<>();

    private ReentrantLock lock = new ReentrantLock();

    /**
     * @author Anson
     * @date 17-8-16
     * @name 获取当前配置信息
     * @since 1.0.6
     */
    public ConfigAccess getConfigAccess() {
        ConfigAccess configAccess = configAccessAtomicReference.get();
        if (configAccess == null) {
            try {
                lock.lock();
                if (configAccess == null) {
                    CommonFacade facade = this.facade("common", CommonFacade.class);
                    configAccess = new DubboFacadeConvertConfigAccess(facade);
                }
            } finally {
                lock.unlock();
            }
        }

        return configAccess;
    }

    /**
     * 是否记录在business状态下的上报地理位置
     */
    @Value("${business.point.validate.algorithm.id:default}")
    private String validateAlgorithmId;

    @Value("${egobus.logo.path}")
    private String egobusLogoPath;

    public long getSessionTimeOut() {
        return this.getConfigAccess().value(USER_LOGIN_SESSION_TIMEOUT.getKey(),
                (Long) USER_LOGIN_SESSION_TIMEOUT.getDefaultValue());
    }

    public Double getSearchMax() {
        return this.getConfigAccess().value(BUSINESS_DISTANCE_SEARCH_MAX.getKey(),
                (Double) BUSINESS_DISTANCE_SEARCH_MAX.getDefaultValue());
    }

    public int getSingleMax() {
        return this.getConfigAccess().value(BUSINESS_DISTANCE_SINGLE_MAX.getKey(),
                (Integer) BUSINESS_DISTANCE_SINGLE_MAX.getDefaultValue());
    }

    public int getQueueLimit() {
        return this.getConfigAccess().value(BUSINESS_STANDBY_MAX.getKey(),
                (Integer) BUSINESS_STANDBY_MAX.getDefaultValue());
    }

    public String getKeyPrefix() {
        return this.getConfigAccess().value(BUSINESS_POINT_REDIS_KEY.getKey(),
                (String) BUSINESS_POINT_REDIS_KEY.getDefaultValue());
    }

    public boolean isStoreBusinessStatusPoint() {
        return this.getConfigAccess().value(BUSINESS_POINT_DO_BUSINESS_IS_STORE.getKey(),
                (Boolean) BUSINESS_POINT_DO_BUSINESS_IS_STORE.getDefaultValue());
    }

    public long driverBusinessGrabTimeout() {
        return this.getConfigAccess().value(DRIVER_BUSINESS_GRAB_TIMEOUT.getKey(),
                (Long) DRIVER_BUSINESS_GRAB_TIMEOUT.getDefaultValue());
    }

    public long userBusinessBusRequestTimeout() {
        return this.getConfigAccess().value(USER_BUSINESS_BUS_REQUEST_TIMEOUT.getKey(),
                (Long) USER_BUSINESS_BUS_REQUEST_TIMEOUT.getDefaultValue());
    }

    public int businessGrabBusDistributeMaxSearchLimit() {
        return this.getConfigAccess().value(BUSINESS_GRAB_BUS_DISTRIBUTE_MAX_SEARCH_LIMIT.getKey(),
                (Integer) BUSINESS_GRAB_BUS_DISTRIBUTE_MAX_SEARCH_LIMIT.getDefaultValue());
    }

    public long bespeakUserCancelValidateLimitSeconds() {
        return this.getConfigAccess().value(BESPEAK_USER_CANCEL_VALIDATE_LIMIT_SECONDS.getKey(),
                (Long) BESPEAK_USER_CANCEL_VALIDATE_LIMIT_SECONDS.getDefaultValue());
    }

    public Long userRequestBespeakMinLimitSeconds() {
        return this.getConfigAccess().value(USER_REQUEST_BESPEAK_MIN_LIMIT_SECONDS.getKey(),
                (Long) USER_REQUEST_BESPEAK_MIN_LIMIT_SECONDS.getDefaultValue());
    }

    public Long userRequestBespeakMaxLimitSeconds() {
        return this.getConfigAccess().value(USER_REQUEST_BESPEAK_MAX_LIMIT_SECONDS.getKey(),
                (Long) USER_REQUEST_BESPEAK_MAX_LIMIT_SECONDS.getDefaultValue());
    }


    public long notifyMsgBeforeBespeakTimeLimitMinutes() {
        return this.getConfigAccess().value(NOTIFY_MSG_BEFORE_BESPEAK_TIME_LIMIT_MINUTES.getKey(),
                (Long) NOTIFY_MSG_BEFORE_BESPEAK_TIME_LIMIT_MINUTES.getDefaultValue());
    }

    public String getEgobusLogoPath() {
        return egobusLogoPath;
    }

    public String getValidateAlgorithmId() {
        return validateAlgorithmId;
    }


}
